package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageParamDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.*;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private ProjectPlanMapper projectPlanMapper;
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    //条件分页查询
    @Override
    public PageResponse<NursingLevelVo> getPage(NursingLevelPageParamDto nursingLevelPageParamDto) {
        //开启分页
        PageHelper.startPage(nursingLevelPageParamDto.getPageNum(), nursingLevelPageParamDto.getPageSize());
        //查询
        Page<NursingLevelVo> result = nursingLevelMapper.selectByPage(nursingLevelPageParamDto);
        //返回结果
        return PageResponse.of(result, NursingLevelVo.class);
    }

    //查询所有
    @Override
    public List<NursingLevelVo> getAllLevel() {
        List<NursingLevelVo> all = nursingLevelMapper.selectAll();
        return all;
    }

    //新增等级
    @Override
    public void addLevel(NursingLevelDto nursingLevelDto) {
        //更换映射
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        //名称校验，不可重复
        Long count = nursingLevelMapper.selectByName(nursingLevel.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.LEVEL_INSERT_FAIL_NAME_REPEAT);
        }
        //新增等级
        nursingLevelMapper.insert(nursingLevel);
    }

    //根据ID查询
    @Override
    public NursingLevelDto getLevelById(Long id) {
        NursingLevelDto level = nursingLevelMapper.selectById(id);
        return level;
    }

    //编辑等级信息
    @Override
    public void updateLevelMsg(NursingLevelDto nursingLevelDto) {
        //更换映射
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
 /*       //不可编辑已绑定老人的等级【nl.planId --> npp.projectId --> nt.elderId】
        *//* level.planId ==> project_plan.projectId *//*
        List<Long> projectIds = projectPlanMapper.selectProjectId(nursingLevel.getPlanId());
        *//* project_plan.projectId ==> nursing_task.elderId *//*
        List<Long> elderIds = nursingTaskMapper.selectElderId(projectIds);
        *//* nursing_task.elderId = elder.id *//*
        Long count = elderMapper.selectElderByLevel(elderIds);
        if (count > 0){
            throw new BaseException(BasicEnum.LEVEL_OPERATE_FAIL_ELDER_EXIT);
        }*/
        //不可编辑已绑定老人的护理等级
        Long count = checkInConfigMapper.selectElderByLevel(nursingLevel.getId());
        if (count > 0){
            throw new BaseException(BasicEnum.LEVEL_OPERATE_FAIL_ELDER_EXIT);
        }
        //编辑信息
        nursingLevelMapper.updateLevel(nursingLevel);
    }

    /**
     *  操作护理等级的校验信息（不可操作已与老人绑定的护理等级）
     *  对于找到绑定的老人的过程，编辑业务中的方式是错的（舍不得删），删除中的是正确的
     */

    //删除护理等级
    @Override
    public void removeLevel(Long id) {
        //不可删除已绑定老人的护理等级
        Long count = checkInConfigMapper.selectElderByLevel(id);
        if (count > 0){
            throw new BaseException(BasicEnum.LEVEL_OPERATE_FAIL_ELDER_EXIT);
        }
        //删除护理等级
        nursingLevelMapper.deleteLevel(id);

        //TODO 将check_in_config表的nursing_level_id更新为null
    }

    //更新护理等级状态
    @Override
    public void updateLevelStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }


}
